{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "本文件为 anywareAI 微信公众号的 《矩阵进阶》 篇的示例代码。\n",
    "\n",
    "本篇内容为：\n",
    "1. 矩阵合并技巧\n",
    "2. 维度叠加技巧"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "metadata": {
    "tags": [],
    "cell_id": "00000-ec8e2395-bc09-4850-b8dc-ee46e57a68e2",
    "output_cleared": false,
    "source_hash": "c2602aa8",
    "execution_millis": 1,
    "execution_start": 1608044822883,
    "deepnote_cell_type": "code"
   },
   "source": [
    "import numpy as np"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": "## np.concatenate",
   "metadata": {
    "tags": [],
    "cell_id": "00001-2856c44c-6683-4526-82e2-fbd9d837d723",
    "output_cleared": false,
    "deepnote_cell_type": "markdown"
   }
  },
  {
   "cell_type": "code",
   "metadata": {
    "tags": [],
    "cell_id": "00001-5c56505c-74a1-486d-a42f-e096e59c27f6",
    "output_cleared": false,
    "source_hash": "6d729911",
    "execution_millis": 2,
    "execution_start": 1608044830212,
    "deepnote_cell_type": "code"
   },
   "source": "arr1 = [1,2,3,4,5,6,7,8]\n\narr2 = [[0,2],[1,3]]\n\nnp.concatenate([arr1[i: j] for i, j in arr2], axis=0)",
   "execution_count": null,
   "outputs": [
    {
     "output_type": "execute_result",
     "execution_count": 4,
     "data": {
      "text/plain": "array([1, 2, 2, 3])"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "tags": [],
    "cell_id": "00003-738f0e39-dc90-4536-b3cf-b1e3960e1703",
    "output_cleared": false,
    "source_hash": "276fec10",
    "execution_start": 1608053709598,
    "execution_millis": 0,
    "deepnote_cell_type": "code"
   },
   "source": "a = [1,2] \nb = [3,4]\nnp.concatenate([a,b], axis=0)",
   "execution_count": null,
   "outputs": [
    {
     "output_type": "execute_result",
     "execution_count": 110,
     "data": {
      "text/plain": "array([1, 2, 3, 4])"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "tags": [],
    "cell_id": "00003-507df8ef-e2d2-40ea-ac0b-06e6d987e1a3",
    "output_cleared": false,
    "source_hash": "48da1596",
    "execution_millis": 0,
    "execution_start": 1608045752609,
    "deepnote_cell_type": "code"
   },
   "source": "[print(str(i)) for i in [[1,2],[3,4]] ]",
   "execution_count": null,
   "outputs": [
    {
     "name": "stdout",
     "text": "[1, 2]\n[3, 4]\n",
     "output_type": "stream"
    },
    {
     "output_type": "execute_result",
     "execution_count": 15,
     "data": {
      "text/plain": "[None, None]"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": "## np.expand_dims",
   "metadata": {
    "tags": [],
    "cell_id": "00003-6381bdc6-4e56-4ac7-aec4-ab5367c63cad",
    "output_cleared": false,
    "deepnote_cell_type": "markdown"
   }
  },
  {
   "cell_type": "code",
   "metadata": {
    "tags": [],
    "cell_id": "00005-71e7da6c-907f-410c-a317-859cb925c5e3",
    "output_cleared": false,
    "source_hash": "d726a8ab",
    "execution_millis": 3,
    "execution_start": 1608046189127,
    "deepnote_cell_type": "code"
   },
   "source": "x = np.array([[[1,2],[3,4]]])\nprint(x.shape)\nx = np.expand_dims(x,axis=(1,2) )\nprint(x.shape)\n",
   "execution_count": null,
   "outputs": [
    {
     "name": "stdout",
     "text": "(1, 2, 2)\n(1, 1, 1, 2, 2)\n",
     "output_type": "stream"
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "tags": [],
    "cell_id": "00005-71270bb9-4574-4f28-904a-410216246dbf",
    "output_cleared": false,
    "source_hash": "ff23c6a0",
    "execution_millis": 0,
    "execution_start": 1608049663302,
    "deepnote_cell_type": "code"
   },
   "source": "a1 = [[1,2],[3,4]]\na2 = [[1,2]]\na3 = []\na4 = []\n\na3.append(a1)\na3.append(a2)\n\na4.append(a3)\na4.append(a3)\nprint(\"a1 shape:\"+str(np.array(a1).shape))\nprint(\"a2 shape:\"+str(np.array(a2).shape))\nprint(\"a3 shape:\"+str(np.array(a3).shape))\nprint(\"a4 shape:\"+str(np.array(a4).shape))\n\n\na1 = [[1,2],[3,4]]\na2 = [[1,2]]\na3 = []\na4 = []",
   "execution_count": null,
   "outputs": [
    {
     "name": "stdout",
     "text": "a1 shape:(2, 2)\na2 shape:(1, 2)\na3 shape:(2,)\na4 shape:(2, 2)\n/opt/venv/lib/python3.7/site-packages/ipykernel_launcher.py:13: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray\n  del sys.path[0]\n/opt/venv/lib/python3.7/site-packages/ipykernel_launcher.py:14: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray\n  \n",
     "output_type": "stream"
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "tags": [],
    "cell_id": "00007-8a629383-f4da-4c0a-a8c1-77031514b0ef",
    "output_cleared": false,
    "source_hash": "8f71d1ac",
    "execution_millis": 0,
    "execution_start": 1608052455077,
    "deepnote_cell_type": "code"
   },
   "source": "a = [[1,2,3],[1,2,3]]\nb = [[4,5,6],[1,2,3],[1,2,3]]\nc = []\nc.append(a)\nc.append(b)\n\nprint(c[:1])\nprint(*c[:1])\n\n\nprint(\"a shape:\"+str(np.array(a).shape))\nprint(\"b shape:\"+str(np.array(b).shape))\nprint(\"c shape:\"+str(np.array(c).shape))\n\nres = [np.concatenate(i, axis=0) for i in zip(*c[:1])]\nprint(\"res shape:\"+str(np.array(res).shape))",
   "execution_count": null,
   "outputs": [
    {
     "name": "stdout",
     "text": "[[[1, 2, 3], [1, 2, 3]]]\n[[1, 2, 3], [1, 2, 3]]\na shape:(2, 3)\nb shape:(3, 3)\nc shape:(2,)\nres shape:(2, 3)\n/opt/venv/lib/python3.7/site-packages/ipykernel_launcher.py:13: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray\n  del sys.path[0]\n",
     "output_type": "stream"
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "tags": [],
    "cell_id": "00008-bf0e070d-46a2-49c0-be44-405c0176ca8b",
    "output_cleared": false,
    "source_hash": "1ae167",
    "execution_millis": 1,
    "execution_start": 1608053639772,
    "deepnote_cell_type": "code"
   },
   "source": "a = [[1,2,3],[1,2,3]]\nb = [[4,5,6],[1,2,3],[1,2,3]]\n\na = np.expand_dims(a,axis=0)\nb = np.expand_dims(b,axis=0)\n\n\nc = []\nc.append(a)\nc.append(b)\n\nd = []\nd.append(c)\nd.append(c)\nd.append(c)\n\nprint(\"a shape:\"+str(np.array(a).shape))\nprint(\"b shape:\"+str(np.array(b).shape))\nprint(\"=================\")\nprint(\"c shape:\")\n[print(np.array(i).shape) for i in c]\nprint(\"=================\")\nprint(\"*d:\")\nprint(*d[:1])\nprint(\"d element shape:\"+str(d[0][0].shape))\nprint(\"-----------------\")\n[print(i) for i in d]\nprint(\"=================\")\nres = [np.concatenate(i, axis=0) for i in zip(*d[:2])]\nprint(res)\nprint(\"-----------------\")\nprint(\"res element shape:\"+str(res[0].shape))",
   "execution_count": null,
   "outputs": [
    {
     "name": "stdout",
     "text": "a shape:(1, 2, 3)\nb shape:(1, 3, 3)\n=================\nc shape:\n(1, 2, 3)\n(1, 3, 3)\n=================\n*d:\n[array([[[1, 2, 3],\n        [1, 2, 3]]]), array([[[4, 5, 6],\n        [1, 2, 3],\n        [1, 2, 3]]])]\nd element shape:(1, 2, 3)\n-----------------\n[array([[[1, 2, 3],\n        [1, 2, 3]]]), array([[[4, 5, 6],\n        [1, 2, 3],\n        [1, 2, 3]]])]\n[array([[[1, 2, 3],\n        [1, 2, 3]]]), array([[[4, 5, 6],\n        [1, 2, 3],\n        [1, 2, 3]]])]\n[array([[[1, 2, 3],\n        [1, 2, 3]]]), array([[[4, 5, 6],\n        [1, 2, 3],\n        [1, 2, 3]]])]\n=================\n[array([[[1, 2, 3],\n        [1, 2, 3]],\n\n       [[1, 2, 3],\n        [1, 2, 3]]]), array([[[4, 5, 6],\n        [1, 2, 3],\n        [1, 2, 3]],\n\n       [[4, 5, 6],\n        [1, 2, 3],\n        [1, 2, 3]]])]\n-----------------\nres element shape:(2, 2, 3)\n",
     "output_type": "stream"
    }
   ]
  }
 ],
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "orig_nbformat": 2,
  "deepnote_notebook_id": "9c0a050c-c805-4064-8bad-13f259350a7e",
  "deepnote_execution_queue": []
 }
}